type NavFn = (to: string, opts?: { replace?: boolean; state?: any }) => void;

let navFn: NavFn | null = null;

export const setNavigate = (fn: NavFn) => {
  navFn = fn;
};

export const navigate = (
  to: string,
  opts?: { replace?: boolean; state?: any }
) => {
  if (navFn) {
    navFn(to, opts);
  } else {
    // fallback：当 Router 还没挂载时，使用 location 做一次硬跳转（可选）
    if (opts?.replace) window.location.replace(to);
    else window.location.href = to;
  }
};
